package com.xhwl.logistics.open;

import com.xhwl.logistics.dto.ResponseCommonPushKeTopDTO;
import com.xhwl.logistics.dto.push.DeviceStatusKeTopDTO;
import com.xhwl.logistics.service.business.VehicleDeviceDataPushBusinessService;
import com.xhwl.logistics.service.impl.redis.VehicleExceptionRedisServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleIntegratedConfigRedisServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleLogRedisServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.concurrent.TimeUnit;

@Slf4j
//@RestController
public class KeTopDevicePushController {

    @Autowired
    private VehicleDeviceDataPushBusinessService vehicleDeviceDataPushBusinessService;

    @Autowired
    private VehicleLogRedisServiceImpl vehicleLogRedisService;

    @Autowired
    private VehicleExceptionRedisServiceImpl vehicleExceptionRedisService;

    @Autowired
    private VehicleIntegratedConfigRedisServiceImpl vehicleIntegratedConfigRedisService;

    @PostMapping("/KeTop/PostDeviceState")
    public ResponseCommonPushKeTopDTO deviceStatus(@RequestBody DeviceStatusKeTopDTO deviceStatusKeTopDTO) {
        log.info("start: ke-top push data for device status");

        final String key = "device-status";
        vehicleLogRedisService.logWithTime(key, deviceStatusKeTopDTO, 1L, TimeUnit.DAYS);

        Integer projectId = vehicleIntegratedConfigRedisService.queryProjectIdByParkId(deviceStatusKeTopDTO.getParkId());

        try {
            vehicleDeviceDataPushBusinessService.keTopDeviceStatus(deviceStatusKeTopDTO, projectId);
        } catch (Exception exception) {
            vehicleExceptionRedisService.exceptionMessage(key, exception.getMessage());
        }

        log.info("end: ke-top push data for device status");
        return ResponseCommonPushKeTopDTO.success();
    }

}
